package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.model.admin.bo.MarketCategoryBo;
import com.cskaoyan.model.admin.bo.MarketCreateCategoryBo;
import com.cskaoyan.model.admin.vo.MarketCategoryChildrenVo;
import com.cskaoyan.model.admin.vo.MarketCategoryL1Vo;
import com.cskaoyan.model.admin.vo.MarketCategoryVo;
import com.cskaoyan.model.admin.vo.MarketCreateCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/11/29 19:53
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public List<MarketCategoryVo> list() {

        //获取第一级别的商品集合
        List<MarketCategory> marketCategoryL1List=marketCategoryMapper.selectByLevel();
        //汇总商品返回(包括第一级别第二级别)
        List<MarketCategoryVo> marketCategoryVoList=new ArrayList<>();


        for (MarketCategory marketCategory : marketCategoryL1List) {
            //获取id为特定值的第二商品集合
            List<MarketCategory>  marketCategoryList=marketCategoryMapper.selectById(marketCategory.getId());
            List<MarketCategoryChildrenVo> marketCategoryChildrenVoList=new ArrayList<>();
            //将数据库所得数据装入第二级别商品返回
            for (MarketCategory category : marketCategoryList) {
                MarketCategoryChildrenVo marketCategoryChildrenVo=new MarketCategoryChildrenVo(category.getPicUrl(),category.getKeywords(),category.getLevel(),category.getName(),category.getId(),category.getIconUrl(),category.getDesc());
                marketCategoryChildrenVoList.add(marketCategoryChildrenVo);
            }
            //将汇总数据装入集合返回
            MarketCategoryVo marketCategoryVo=new MarketCategoryVo(marketCategory.getPicUrl(),marketCategory.getKeywords(),marketCategory.getLevel(),marketCategoryChildrenVoList,marketCategory.getName(),marketCategory.getId(),marketCategory.getIconUrl(),marketCategory.getDesc());
            marketCategoryVoList.add(marketCategoryVo);
        }

        //返回
        return marketCategoryVoList;
    }

    @Override
    public List<MarketCategoryL1Vo> list2() {
        List<MarketCategory> marketCategoryL1List=marketCategoryMapper.selectByLl();
        List<MarketCategoryL1Vo> marketCategoryL1VoList =new ArrayList<>();
        for (MarketCategory marketCategory : marketCategoryL1List) {
            MarketCategoryL1Vo marketCategoryL1Vo=new MarketCategoryL1Vo(marketCategory.getName(),marketCategory.getId());
            marketCategoryL1VoList.add(marketCategoryL1Vo);
        }
        return marketCategoryL1VoList;
    }

    @Override
    public String updateMarketCategory(MarketCategoryBo marketCategoryBo) {
        MarketCategory marketCategory1 = marketCategoryMapper.selectMarketCategoryById(marketCategoryBo.getId());
        MarketCategory marketCategory=marketCategoryBo;
        marketCategoryMapper.updateMarketCategory(marketCategory);

        return marketCategory1.getLevel();

    }

    @Override
    public void deleteMarketCategory(MarketCategoryBo marketCategoryBo) {
        MarketCategory marketCategory=new MarketCategory();
        marketCategory.setId(marketCategoryBo.getId());
        marketCategoryMapper.deleteMarketCategory(marketCategory);
    }
    @Override
    public MarketCreateCategoryVo createMarketCategory(MarketCreateCategoryBo marketCreateCategoryBo) {
        MarketCategory marketCategory=new MarketCategory();
        marketCategory.setName(marketCreateCategoryBo.getName());
        marketCategory.setDesc(marketCreateCategoryBo.getDesc());
        marketCategory.setKeywords(marketCreateCategoryBo.getKeywords());
        marketCategory.setPicUrl(marketCreateCategoryBo.getPicUrl());
        marketCategory.setLevel(marketCreateCategoryBo.getLevel());
        marketCategory.setPid(marketCreateCategoryBo.getPid());
        marketCategory.setIconUrl(marketCreateCategoryBo.getIconUrl());
        marketCategoryMapper.createMarketCategory(marketCategory);
        marketCategory=marketCategoryMapper.selectMarketCategoryById(marketCategory.getId());
        MarketCreateCategoryVo marketCreateCategoryVo=new MarketCreateCategoryVo(marketCategory.getPicUrl(),marketCategory.getKeywords(),marketCategory.getAddTime(),marketCategory.getLevel(),marketCategory.getName(),marketCategory.getPid(),marketCategory.getUpdateTime(),marketCategory.getId(),marketCategory.getIconUrl(),marketCategory.getDesc());
        return marketCreateCategoryVo;
    }
}
